package com.binhai.springboot.person.service;

import com.binhai.springboot.person.bean.PersonBean;
import com.binhai.springboot.person.dao.PersonBeanDao;
import com.binhai.springboot.user.bean.UserBean;
import com.binhai.springboot.user.dao.UserBeanDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PersonService {
    @Autowired
    private UserBeanDao beanDao;


    @Autowired
    private PersonBeanDao postDao;

    @Autowired
    private HttpSession session;

    public static int UserId;
    /**
     * 查询所有数据
     * @param
     * @return
     */
    public List getAllData() {

        return postDao.getAllData();
    }


    /**
     * 保存信息
     * @param bean
     * @return
     */
    public Map saveData(PersonBean bean) {
        Map re = new HashMap();
        try {
            //保存
          postDao.insertSelective(bean);

            re.put("result", "success");
        } catch (Exception e) {
            re.put("result", "error");
            e.printStackTrace();
        }
        return re;
    }

    /**
     * 修改
     * @param bean
     * @return
     */

    public Map updateData(PersonBean bean) {
        Map re = new HashMap();
        try {
            postDao.update(bean);

            re.put("result", "success");
        } catch (Exception e) {
            re.put("result", "error");
            e.printStackTrace();
        }
        return re;
    }

    public Map deleteData(PersonBean bean) {
        Map re = new HashMap();
        try {
            postDao.delete(bean);

            re.put("result", "success");
        } catch (Exception e) {
            re.put("result", "error");
            e.printStackTrace();
        }
        return re;
    }

    public List selectData(String name) {
        return postDao.select(name);
    }

    public String person2(PersonBean bean) {
        postDao.person2 (bean);
        return "login";
    }
    public String login(UserBean bean,String code,PersonBean beanbean) {
        String sessionCode = (String) session.getAttribute ("code");

        UserBean user = beanDao.login (bean);
        PersonBean person = postDao.person (beanbean);

        UserId = beanDao.selectUP (bean);
        System.out.println (UserId);
        if (user != null && sessionCode.equalsIgnoreCase (code)) {
//            登录成功 记录用户信息
            session.setAttribute ("user", user);
            session.setAttribute ("person", person);
            return "index";
        } else {
            return "login";
        }
    }
}
